﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace SPNetworkTopology.ExternalWindows.ViewModel
{
    class SPColumnSelectionViewModel : DataLinkingViewModelBase
    {
        List<OptionViewModel<string>> sharePointColumnCollection;
        bool isValid = true;

        public List<OptionViewModel<string>> SPColumnCollection
        {
            get
            {
                if (sharePointColumnCollection == null)
                {
                    this.CreateSPColumnCreation();
                }
                return sharePointColumnCollection;
            }
        }

        private void CreateSPColumnCreation()
        {
            sharePointColumnCollection = new List<OptionViewModel<string>>();

            List<string> columnCollection = this.DataLinking.ExpectedSPColumns.ToList();
            columnCollection.Insert(0, "Select All");

            foreach (string column in columnCollection)
            {
                OptionViewModel<string> columnCol = new OptionViewModel<string>(column, column);
                columnCol.PropertyChanged += new PropertyChangedEventHandler(columnCol_PropertyChanged);

                if (column.Equals("Server Name") || 
                    column.Equals("Service Name") ||
                    column.Equals("Object Type"))
                {
                    columnCol.IsSelected = true;
                }

                sharePointColumnCollection.Add(columnCol);
            }
        }

        void columnCol_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            OptionViewModel<string> option = sender as OptionViewModel<string>;
            SelectionChangedAction(option, option.IsSelected);

            this.isValid = !this.DataLinking.SelectedColumns.Count.Equals(0);
        }

        private void SelectionChangedAction(OptionViewModel<string> option, bool isSelected)
        {
            if (option.GetValue().Equals("Select All"))
            {
                sharePointColumnCollection.ForEach(item =>
                {
                    if (!item.DisplayName.Equals("Server Name") && 
                        !item.DisplayName.Equals("Service Name") &&
                        !item.DisplayName.Equals("Object Type"))
                    {
                        item.IsSelected = isSelected;
                    }
                });
                return;
            }
            else if (isSelected)
            {
                this.DataLinking.SelectedColumns.Add(option.GetValue());
                return;
            }

            this.DataLinking.SelectedColumns.Remove(option.GetValue());
        }

        public SPColumnSelectionViewModel(DataLinking dataLinking)
            : base(dataLinking)
        {
            this.DataLinking.SelectedColumns = new List<string>();
            this.isValid = !this.DataLinking.SelectedColumns.Count.Equals(0);
        }

        public override string DisplayName
        {
            get { return SPNetworkTopologyResource.SharePointColumnsSelectionHeader; }
        }

        internal override bool IsValid()
        {
            return this.isValid;
        }
    }
}
